package exam.controller;

import exam.bean.UserInfo;
import exam.service.IUserInfoService;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("UserInfo")
@RestController()
public class UserInfoController {
    @Autowired
    IUserInfoService userInfoService;


    @PreAuthorize("hasAuthority('sys:userInfo:list')")
    @ResponseBody
    @RequestMapping(value = "/UserInfoSelect",method = {RequestMethod.POST,RequestMethod.GET})
    public Msg Student(
            @RequestParam(value="pageNum",defaultValue="1") int pageNum,
            @RequestParam(value="pageSize",defaultValue = "10") int pageSize,
            @RequestParam(value = "name",defaultValue = "") String name){
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("name",name);
        return Msg.success().add("pageInfo",userInfoService.selectInfo(pageNum,pageSize,queryMap));
    }

    @PreAuthorize("hasAuthority('sys:userInfo:add')")
    @ResponseBody
    @PostMapping("/add")
    public Msg addInfo(@RequestBody UserInfo userInfo){
        boolean re =  userInfoService.insertInfo(userInfo) > 0 ;
        return re ? Msg.success().setMessage("添加成功") : Msg.error().setMessage("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:userInfo:update')")
    @ResponseBody
    @PostMapping("/upDate")
    public Msg updateInfo(@RequestBody UserInfo userInfo){
        boolean re =  userInfoService.updateInfo(userInfo) > 0;
        return re ? Msg.success().setMessage("更新成功") : Msg.error().setMessage("更新失败");
    }

    @PreAuthorize("hasAuthority('sys:userInfo:delete')")
    @ResponseBody
    @GetMapping("/delete")
    public Msg deleteInfo(@RequestParam(value="id") int id){

        boolean re = userInfoService.deleteInfo(id) > 0;
        return re ? Msg.success().setMessage("删除成功") : Msg.error().setMessage("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:userInfo:detail')")
    @ResponseBody
    @GetMapping("/detail")
    public Msg detailInfo(@RequestParam(value="id") int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        UserInfo userInfo = userInfoService.selectInfoSingle(map);
        userInfo.setPassword("");
        return Msg.success().add("userInfo",userInfo);
    }
}
